//
// Created by pzw on 23-12-27.
//

#include "VpnConnection.h"
#include <iostream>
#include <string>
#include <cstdio>

std::string getOpenVPNStatus() {
    std::string command = "sudo systemctl status openvpn.service";
    std::string result = "";
    FILE *pipe = popen(command.c_str(), "r");
    if (pipe) {
        char buffer[128];
        while (!feof(pipe)) {
            if (fgets(buffer, 128, pipe) != nullptr) {
                result += buffer;
            }
        }
        pclose(pipe);
    }
    return result;
}

std::string getOpenVPNConnectionStatus() {
    std::string status = getOpenVPNStatus();
    if (status.find("active") != std::string::npos) {
        return "已连接";
    } else if (status.find("inactive") != std::string::npos) {
        return "已断开";
    } else {
        return "连接中";
    }
}

int vpn_connection_main() {
    std::string connectionStatus = getOpenVPNConnectionStatus();
    std::cout << "连接状态: " << connectionStatus << std::endl;
    return 0;
}